/*
package com.zky.gulimall.product.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.UUID;

*/
/**
 * @Description TODO
 * @Author 张开源
 * @Date 2022/7/19 21:08
 * @Version 1.0
 **//*

@Slf4j
@Component
public class AliyunOSSUtil {

    @Value("${oss.endpoint}")
    private String endpoint;

    @Value("${oss.bucketName}")
    private String bucketName;

    @Value("${oss.accessKeyId}")
    private String accessKeyId;

    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;

    private  OSSClient client;


    */
/**
     * 上传文件
     *//*

    public String upLoad(File file) {
        log.info("------OSS文件上传开始--------" + file.getName());
        //这里endpoint 在你的bucket列表->点击你的bucket->点击概览中间就有，下面有截图
        System.out.println("获取到的Point为:" + endpoint);

        // 判断文件
        if (file == null) {
            return null;
        }
        client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            // 判断容器是否存在,不存在就创建
            boolean b = !client.doesBucketExist(bucketName);
            System.out.println("doesBucketExist:"+b);
            if (b) {
                client.createBucket(bucketName);
                CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                client.createBucket(createBucketRequest);
            }
            // 设置文件路径和名称
            String fileUrl = UUID.randomUUID().toString().replace("-", "") + "-" + file.getName();
            // 上传文件
            PutObjectResult result = client.putObject(new PutObjectRequest(bucketName, fileUrl, file));
            // 设置权限(公开读)
            client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            if (result != null) {
                log.info("------OSS文件上传成功------" + "endpoint:"+endpoint +"/"+"fileUrl:"+ fileUrl);
                return getUrl(fileUrl);
            }
        } catch (OSSException oe) {
            log.error(oe.getMessage());
        } catch (ClientException ce) {
            log.error(ce.getErrorMessage());
        } finally {
            if (client != null) {
                client.shutdown();
            }
        }
        return null;
    }

    */
/**
     * 获得url链接
     *
     * @param key
     * @return
     *//*

    public String getUrl(String key) {
        // 设置URL过期时间为10年 3600l* 1000*24*365*10
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10);
        // 生成URL
        URL url = client.generatePresignedUrl(bucketName, key, expiration);
        if (url != null) {
            return url.toString();
        }
        return null;
    }

}
*/
